// pages/release/relase.ts
import { getDynamicMixin } from "@mixin/dynamic"
import { tipMixin } from "@mixin/tip"
import { dynamicStore } from "@store/dynamic"
import {uploadfile} from "@utils/http"
import { createStoreBindings } from 'mobx-miniprogram-bindings'
Page({
  mixins:[tipMixin,getDynamicMixin],

  /**
   * 页面的初始数据
   */
  data: {
    form:{
      files:[],
      content:"",
      keyword:[],
      provided:false,
      anonymous:false,
      contact:""
    },
    showTagInput:false,
    tagColors:[
      {bg:"var(--primary-3)",text:"var(--primary-7)"},
      {bg:"var(--green-3)",text:"var(--green-7)"},
      {bg:"var(--fill-color-3)",text:"var(--text-color-1)"}
    ],
    showLoading:false
  },
  tip(message:string,type?:string){},
  verify(){
    const form:any={...this.data.form}
    if(!form.provided){
      delete form.contact
    }
    if(Object.values(form).some(item=>{
      return typeof item==="string"&&!item||Array.isArray(item)&&item.some(item=>{
        return typeof item==="string"&&!item
      })
    })){
      this.tip("请填写表格需收集的信息")
      return false
    }
    return true
  },
  submit:async function(){
    if(!this.verify())return
    this.setData({
      showLoading:true
    })
    await uploadfile("/dynamics",this.data.form,"files")
    this.resetDynamicList()
    this.getdynamic()
      this.setData({
        showLoading:false
      })
      wx.showModal({
        content: '发布成功',
        showCancel: false,
        confirmText: '确定',
        confirmColor: 'var(--primary-6)',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack({
              delta:1
            })
          }
        }
      });
    
  },
  saveFormData:function(e:any){
    const key=e.currentTarget.dataset.key
    const value=e.detail
    const form=this.data.form
    form[key]=value
    this.setData({
      form
    })
  },
  deleteTag:function(e:any){
    const index=e.currentTarget.dataset.index
    const form=this.data.form
    form.keyword.splice(index,1)
    this.setData({
      form
    })
  },
  addtag:function(e:any){
    const tag=e.detail.value
    if(tag.trim()){
      const form=this.data.form
    form.keyword.push(tag)
    this.setData({
      form
    })
    }
    this.setData({
      showTagInput:false
    })
  },
  showaddtag:function(){
    this.setData({
      showTagInput:!this.data.showTagInput
    })
  },
  handleUpload:function(e:any){
    const form=this.data.form
    form.files=[...this.data.form.files,...e.detail.file]
    this.setData({
     form
    })
  },
  deleteFile:function(e:any){
    const form=this.data.form
    form.files.splice(e.detail.index,1)
    this.setData({
      form
    })
  },
  onLoad(){
    this.storeBindings=createStoreBindings(this,{
      store:dynamicStore,
      actions:["resetDynamicList","addDynamicList"]
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})